package com.ewzj.bigdata.mvp.ui.adapter;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ewzj.bigdata.R;
import com.ewzj.bigdata.mvp.model.entity.resbean.CommentBean;
import com.ewzj.bigdata.mvp.ui.widget.CommentListTextView;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;

import org.w3c.dom.Comment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author:zhangjing
 * Time:2018/3/21  下午3:59
 * Name: this is CommentAdapter
 * Email:zhangjingworkmol@163.com
 */

public class CommentAdapter extends BaseQuickAdapter<CommentBean, BaseViewHolder> {
    private ImageLoader imageLoader;
    private CommentPartClickListener commentListener;
    public CommentAdapter(@Nullable List<CommentBean> data) {
        super(R.layout.item_comment, data);
    }

    public CommentPartClickListener getCommentListener() {
        return commentListener;
    }

    public void setCommentListener(CommentPartClickListener commentListener) {
        this.commentListener = commentListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, CommentBean item) {
        if (imageLoader==null){
            imageLoader=ArmsUtils.obtainAppComponentFromContext(mContext).imageLoader();
        }
//        if (TextUtils.isEmpty(item.getHeadimg())){
            imageLoader.loadImage(mContext, ImageConfigImpl.builder()
                    .imageView(helper.<ImageView>getView(R.id.iv_userimg))
                    .transformation(new CircleCrop())
                    .url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1521647774227&di=a5f6fdd76f0172ada180863ec18dbd45&imgtype=0&src=http%3A%2F%2Fp5.qhmsg.com%2Ft017e57ca07889e729a.png")
                    .build());
//        }
        helper.<ImageView>getView(R.id.iv_like).setSelected(item.isIs_praise());
        helper.<TextView>getView(R.id.tv_usrname).setText(item.getFrom_user_name());
        helper.<TextView>getView(R.id.tv_comment).setText(item.getContent());
        helper.<TextView>getView(R.id.tv_like).setText(item.getPraise_num()+"");
        helper.<TextView>getView(R.id.tv_comment_num).setText(item.getComment_num()+"");
        helper.addOnClickListener(R.id.ll_like);
        helper.addOnClickListener(R.id.ll_comment);
        if (item.getList()!=null&&item.getList().isEmpty()){
            helper.getView(R.id.line).setVisibility(View.GONE);
        }else {
            helper.getView(R.id.line).setVisibility(View.VISIBLE);
        }
        initCommentView(helper.<CommentListTextView>getView(R.id.conmmentview),item);
    }

    /**
     * 实在不想写了
     * @param mCommentListTextView
     * @param item
     */
    private void initCommentView(CommentListTextView mCommentListTextView, CommentBean item) {
        mCommentListTextView.setMaxlines (item.getCommentLineNum());
        mCommentListTextView.setMoreStr ("展开全部评论");
        mCommentListTextView.setNameColor (Color.parseColor ("#569bd5"));
        mCommentListTextView.setCommentColor (Color.parseColor ("#4a4a4a"));
        mCommentListTextView.setTalkStr ("回复");
        mCommentListTextView.setTalkColor (Color.parseColor ("#000000"));
        if (item.getList()!=null&&!item.getList().isEmpty()){
            mCommentListTextView.setData(comment2CommentInfo(item));
        }
        mCommentListTextView.setonCommentListener(new CommentListTextView.onCommentListener() {
            @Override
            public void onNickNameClick(int position, CommentListTextView.CommentInfo mInfo) {
                if (commentListener!=null){
                    commentListener.onNickNameClick(position,mInfo);
                }
            }

            @Override
            public void onToNickNameClick(int position, CommentListTextView.CommentInfo mInfo) {
                if (commentListener!=null){
                    commentListener.onToNickNameClick(position,mInfo);
                }
            }

            @Override
            public void onCommentItemClick(int position, CommentListTextView.CommentInfo mInfo) {
                if (commentListener!=null){
                    commentListener.onCommentItemClick(position,mInfo);
                }
            }

            @Override
            public void onOtherClick() {
                item.setCommentLineNum(10);
                notifyDataSetChanged();
            }
        });
    }
    private List<CommentListTextView.CommentInfo> comment2CommentInfo(CommentBean item){
        List<CommentListTextView.CommentInfo> mCommentInfos = new ArrayList<>();
        for (int i = 0; i < item.getList().size(); i++) {
            CommentBean.ListEntity listEntity=item.getList().get(i);
            CommentListTextView.CommentInfo commentInfo=new CommentListTextView.CommentInfo();
            commentInfo.setID(listEntity.getId()).setComment(listEntity.getContent()).setNickname(listEntity.getFrom_user_name()).setTonickname(listEntity.getTo_user_name());
            mCommentInfos.add(commentInfo);
        }
        return mCommentInfos;
    }
    public interface CommentPartClickListener{
        void  onNickNameClick(int position, CommentListTextView.CommentInfo mInfo);
        void  onToNickNameClick(int position, CommentListTextView.CommentInfo mInfo);
        void onCommentItemClick(int position, CommentListTextView.CommentInfo mInfo);
    }

}
